/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.sys.comment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yami.shop.bean.enums.PositionType;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.sys.comment.dao.ShopEmployeeMapper;
import com.yami.shop.sys.comment.dao.ShopMenuMapper;
import com.yami.shop.sys.comment.dao.ShopRoleMenuMapper;
import com.yami.shop.sys.comment.model.ShopEmployee;
import com.yami.shop.sys.comment.model.ShopMenu;
import com.yami.shop.sys.comment.service.ShopMenuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单管理
 * @author yami
 * @date 2021-03-01 17:42:09
 */
@Service
@AllArgsConstructor
public class ShopMenuServiceImpl extends ServiceImpl<ShopMenuMapper, ShopMenu> implements ShopMenuService {

    private final ShopMenuMapper shopMenuMapper;
    private final ShopRoleMenuMapper shopRoleMenuMapper;
    private final ShopEmployeeMapper shopEmployeeMapper;

    @Override
    public List<ShopMenu> listMenuAndBtn() {
        return shopMenuMapper.listMenuAndBtn(I18nMessage.getLang());
    }

    @Override
    public List<ShopMenu> listSimpleMenuNoButton() {
        return shopMenuMapper.listSimpleMenuNoButton(I18nMessage.getLang());
    }

    @Override
    public List<ShopMenu> listRootMenu() {
       return shopMenuMapper.listRootMenu(I18nMessage.getLang());
    }

    @Override
    public List<ShopMenu> listChildrenMenuByParentId(Long parentId) {
        return shopMenuMapper.listChildrenMenuByParentId(parentId,I18nMessage.getLang());
    }

    @Override
    public void deleteMenuAndRoleMenu(Long menuId) {
        //删除菜单
        this.removeById(menuId);
        //删除菜单与角色关联
        shopRoleMenuMapper.deleteByMenuId(menuId);
    }

    @Override
    public List<ShopMenu> listMenuByEmployeeId(Long employeeId) {
        // 用户的所有菜单信息
        List<ShopMenu> shopMenus;
        //商铺超级管理员，拥有最高权限
        ShopEmployee shopEmployee = shopEmployeeMapper.selectById(employeeId);
        if(Objects.equals(PositionType.ADMIN.value(),shopEmployee.getType())){
            shopMenus = shopMenuMapper.listMenu(I18nMessage.getLang());
        }else {
            shopMenus = shopMenuMapper.listMenuByEmployeeId(employeeId,I18nMessage.getLang());
        }
        Map<Long, List<ShopMenu>> sysMenuLevelMap = shopMenus.stream().sorted(Comparator.comparing(ShopMenu::getOrderNum))
                .collect(Collectors.groupingBy(ShopMenu::getParentId));

        // 一级菜单
        List<ShopMenu> rootMenu = sysMenuLevelMap.get(0L);
        if (CollectionUtil.isEmpty(rootMenu)) {
            return Collections.emptyList();
        }
        // 二级菜单
        for (ShopMenu sysMenu : rootMenu) {
            sysMenu.setList(sysMenuLevelMap.get(sysMenu.getMenuId()));
        }
        return rootMenu;
    }

    @Override
    public List<Long> listMenuIdByRoleId(Long roleId) {
        return shopMenuMapper.listMenuIdByRoleId(roleId);
    }
}
